package com.veo.controller.admin;

import com.veo.entity.Tag;
import com.veo.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("/admin")
public class TagController {

    @Autowired
    private TagService tagService;

    //分页查询，默认参数为，每页3条，按id倒序排序
    @GetMapping("/tags")
    public String types(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable, Model model){
        Page<Tag> tagPage = tagService.listTag(pageable);
        model.addAttribute("page",tagPage);
        return "admin/tags";
    }

    //跳转添加分类页面
    @GetMapping("/tags/input")
    public String input(Model model){
        model.addAttribute("tag",new Tag());
        return "admin/tags-input";
    }

    //保存分类,bindResult必须在要校验的类型的后面
    @PostMapping("/tags")
    public String post(@Valid Tag tag, BindingResult result, RedirectAttributes attributes){
        Tag tagByName = tagService.getTagByName(tag.getName());
        if (tagByName != null){
            //自定义错误，name 必须和type类中校验字段的名称相同
            result.rejectValue("name","nameError","不能重复添加标签");
        }

        //服务器端校验，有错就返回
        if (result.hasErrors()){
            return "admin/tags-input";
        }
        Tag t = tagService.saveTag(tag);
        if (t == null){
            //分类添加失败，返回失败信息
            attributes.addFlashAttribute("msg","新增失败");
        }else {
            //分类添加成功，返回成功信息
            attributes.addFlashAttribute("msg","新增成功");
        }
        //跳转查询后的页面
        return "redirect:/admin/tags";
    }

    //修改分类
    @PostMapping("/tags/{id}")
    public String editPost(@Valid Tag tag, BindingResult result,@PathVariable(name = "id") Long id, RedirectAttributes attributes){
        Tag tagByName = tagService.getTagByName(tag.getName());
        if (tagByName != null){
            //自定义错误，name 必须和type类中校验字段的名称相同
            result.rejectValue("name","nameError","不能重复添加分类");
        }

        //服务器端校验，有错就返回
        if (result.hasErrors()){
            return "admin/tags-input";
        }
        Tag t = tagService.updateTag(id, tag);
        if (t == null){
            //分类添加失败，返回失败信息
            attributes.addFlashAttribute("msg","更新失败");
        }else {
            //分类添加成功，返回成功信息
            attributes.addFlashAttribute("msg","更新成功");
        }
        //跳转查询后的页面
        return "redirect:/admin/tags";
    }

    //删除分类
    @GetMapping("/tags/{id}/delete")
    public String delete(@PathVariable(name = "id") Long id,RedirectAttributes attributes){
        tagService.deleteTag(id);
        attributes.addFlashAttribute("msg","删除成功");
        return "redirect:/admin/tags";
    }

    //跳转到修改界面
    @GetMapping("/tags/{id}/input")
    public String editInput(@PathVariable(name = "id") Long id, Model model){
        model.addAttribute("tag",tagService.getTag(id));
        return "admin/tags-input";
    }
}
